package cn.tedu.jdbc;
import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class DBUtil {
    private static final String DATABASE,IP;
    private static DruidDataSource dds;
    static {
        DATABASE = "student_info";
        IP = "localhost:3306";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            dds = new DruidDataSource();
            dds.setUrl("jdbc:mysql://"+IP+"/"+DATABASE+"?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
            dds.setUsername("root");
            dds.setPassword("123456");
            dds.setInitialSize(5); // 初始连接数
            dds.setMaxActive(10); // 最大连接数
            dds.init(); // 初始化
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public static Connection getConnection() throws  SQLException {
        return dds.getConnection();
    }
}
